// settings.js
Page({
  goHome() {
    wx.navigateBack();
  },
    data: {
      mqttHost: '',
      deviceId: '',
      deviceKey: ''
    },
  
    onLoad() {
      const mqttConfig = wx.getStorageSync('mqttConfig') || {};
      this.setData({
        mqttHost: mqttConfig.host || '',
        deviceId: mqttConfig.deviceId || '',
        deviceKey: mqttConfig.deviceKey || ''
      });
    },
  
    setMqttHost(e) {
      this.setData({ mqttHost: e.detail.value });
    },
  
    setDeviceId(e) {
      this.setData({ deviceId: e.detail.value });
    },
  
    setDeviceKey(e) {
      this.setData({ deviceKey: e.detail.value });
    },
  
    saveSettings() {
      const { mqttHost, deviceId, deviceKey } = this.data;
      
      if (!mqttHost || !deviceId || !deviceKey) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
        return;
      }
      
      wx.setStorageSync('mqttConfig', {
        host: mqttHost,
        deviceId: deviceId,
        deviceKey: deviceKey
      });
      
      wx.showToast({
        title: '保存成功',
        icon: 'success'
      });
      
      // 通知主页面重新连接
      getApp().globalData.reconnectMQTT = true;
      wx.navigateBack();
    }
  });